﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using cart.model;
using cart.utils;

namespace cart.dao
{
    public class orderDao
    {
        public Boolean addOrder(orderModel om)
        {
            string INSERT_INTO_ORDER = "INSERT INTO [order] VALUES (@orderID,@userID,@userName,@userAddr,@phone,@postType,@orderState,@paymentState,@addOrderTime,@totle,@zip)";
            SqlParameter[] parms = new SqlParameter[] 
            {
                new SqlParameter("@orderID",DbType.Int32),
                new SqlParameter("@userID",DbType.String),
                new SqlParameter("@userName",DbType.String),
                new SqlParameter("@userAddr",DbType.String),
                new SqlParameter("@phone",DbType.String),
                new SqlParameter("@postType",DbType.String),
                new SqlParameter("@orderState",DbType.Int32),
                new SqlParameter("@paymentState",DbType.Int32),
                new SqlParameter("@addOrderTime",DbType.DateTime),
                new SqlParameter("@totle",DbType.Double),
                new SqlParameter("@zip",DbType.String)
            };
            setOrderSqlParameter(parms, om);
            return DB.executeNonQuerySQL(INSERT_INTO_ORDER, parms);
        }

        /// <summary>
        /// 根据订单id更新订单
        /// </summary>
        /// <param name="om"></param>
        /// <returns></returns>
        public Boolean updateOrderByOrderID(orderModel om)
        {
            string UPDATE_ORDER_BY_ORDERID = "UPDATE [order] SET  userName = @userName,userAddr = @userAddr,phone = @phone,postType = @postType,zip = @zip,paymentState = @paymentState WHERE orderID=@orderID";

            SqlParameter[] parms = new SqlParameter[] 
            {
                new SqlParameter("@userName",om.username),
                new SqlParameter("@userAddr",om.userAddr),
                new SqlParameter("@phone",om.phone),
                new SqlParameter("@postType",om.postType),
                new SqlParameter("@zip",om.zip),
                new SqlParameter("@paymentState",om.paymentState),
                new SqlParameter("@orderID",om.orderID)
            };
            return DB.executeNonQuerySQL(UPDATE_ORDER_BY_ORDERID, parms);
        }


        /// <summary>
        /// 得到order
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public orderModel getOrderByID(int orderID)
        {
            string SELECT_ORDER_BY_ORDERID = "select * from [order] where orderID = @orderID";
            orderModel om = new orderModel();
            SqlConnection conn = DB.getSqlConnection();
            DB.connectionOpen(conn);
            SqlCommand cmd = new SqlCommand(SELECT_ORDER_BY_ORDERID, conn);
            cmd.Parameters.AddWithValue("@orderID", orderID);
            SqlDataReader sdr = cmd.ExecuteReader();
            if (sdr.Read())
            {
                om.orderID = sdr.GetInt32(0);
                om.userID = sdr.GetString(1);
                om.username = (sdr.IsDBNull(2)) ? null : sdr.GetString(2).Trim();
                om.userAddr = (sdr.IsDBNull(3)) ? null : sdr.GetString(3).Trim();
                om.phone = (sdr.IsDBNull(4)) ? null : sdr.GetString(4).Trim();
                om.postType = (sdr.IsDBNull(5)) ? null : sdr.GetString(5).Trim();
                om.orderState = (sdr.IsDBNull(6)) ? 0 : sdr.GetInt32(6);
                om.paymentState = (sdr.IsDBNull(7)) ? 0 : sdr.GetInt32(7);
                om.addOrderTime = sdr.GetDateTime(8);
                om.totle = (sdr.IsDBNull(9)) ? 0 : Convert.ToDouble(sdr.GetValue(9));
                om.zip = (sdr.IsDBNull(10)) ? null : sdr.GetString(10).Trim();
            }
            DB.connectionClose(conn);
            return om;
        }

        /// <summary>
        /// 设置参数
        /// </summary>
        /// <param name="parms"></param>
        /// <param name="om"></param>
        private void setOrderSqlParameter(SqlParameter[] parms,orderModel om)
        {
            parms[0].Value = om.orderID;
            parms[1].Value = om.userID;

            if (null == om.username)
                parms[2].Value = System.DBNull.Value;
            else
                parms[2].Value = om.username;

            if (null == om.userAddr)
                parms[3].Value = System.DBNull.Value;
            else
                parms[3].Value = om.userAddr;

            if (null == om.phone)
                parms[4].Value = System.DBNull.Value;
            else
                parms[4].Value = om.phone;

            if (null == om.postType)
                parms[5].Value = System.DBNull.Value;
            else
                parms[5].Value = om.postType;

                parms[6].Value = om.orderState;
                parms[7].Value = om.paymentState;

            if (null == om.addOrderTime)
                parms[8].Value = DateTime.Now;
            else
                parms[8].Value = om.addOrderTime;

            parms[9].Value = om.totle;

            if (null == om.zip)
                parms[10].Value = System.DBNull.Value;
            else
                parms[10].Value = om.zip;

        }
    }
}
